package com.gitee.feizns.explore.data_structure.graph;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 841. 钥匙和房间
 * @author feizns
 * @since 2020/5/7
 */
public class CanVisitAllRooms {

    public static void main(String[] args) {

    }

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        this.graph = rooms;
        return dfs(0) == rooms.size();
    }

    List<List<Integer>> graph;

    Set<Integer> visited = new HashSet<>();

    private int dfs(int x) {
        int ret = 1;
        visited.add(x);
        List<Integer> adj = graph.get(x);
        for (int v : adj) {
            if ( !visited.contains(v) )
                ret += dfs(v);
        }
        return ret;
    }

}
